<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "mkt_post_complain".
 *
 * @property string $id
 * @property string $user_id
 * @property string $post_id
 * @property integer $reason
 */
class MktPostComplain extends \common\wosotech\base\ActiveRecord
{

    const POST_COMPLAIN_CHEAT = 0;
    const POST_COMPLAIN_AD = 1;
    const POST_COMPLAIN_PROMOTION = 2;
    const POST_COMPLAIN_PRIVATE = 3;
    const POST_COMPLAIN_COPYRIGHT = 4;    

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_post_complain';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'post_id', 'reason'], 'integer'],
//            [['user_id', 'post_id'], 'unique', 'targetAttribute' => ['user_id', 'post_id'], 'message' => '已投诉此贴.']
        ];
    }

    public function getUser()
    {
        return $this->hasOne(MktUser::className(), ['id' => 'user_id']);
    }

    public function getPost()
    {
        return $this->hasOne(MktPost::className(), ['id' => 'post_id']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '投诉者',
            'post_id' => '贴子ID',
            'reason' => '理由',         //  0:欺诈, 1:广告骚扰, 2:恶意营销, 3:隐私信息收集, 4:侵权
            'create_time' => '时间',            
        ];
    }

    /**
     * @inheritdoc
     * @return MktPostComplainQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktPostComplainQuery(get_called_class());
    }

    public function behaviors() {
        return [
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'createdAtAttribute' => 'create_time',
                'updatedAtAttribute' => 'create_time',
                'value' => new \yii\db\Expression('NOW()'),
            ],
        ];
    }

    public static function getPostComplainOption($key = null)
    {
        $arr = array(
            static::POST_COMPLAIN_CHEAT => '欺诈',
            static::POST_COMPLAIN_AD => '广告骚扰',
            static::POST_COMPLAIN_PROMOTION => '恶意营销',
            static::POST_COMPLAIN_PRIVATE => '隐私信息收集',            
            static::POST_COMPLAIN_COPYRIGHT => '侵权（冒名、诽谤、抄袭）',                        
        );        
        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

/*
    public static function insertAjax($params)
    {
        $model = new MktPostComplain();
        $model->loadDefaultValues();
        if ($model->load($params, '') && $model->save()) {
            return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);	        
        }        
        yii::error(print_r($model->getErrors(), true));
        return \yii\helpers\Json::encode(['code' => 1, 'data' => '保存错误']);                
    }
*/    
}
